"""
管理后台-组织机构相关接口
"""

from fastapi import APIRouter, Depends, Query

from app.core.deps import CurrentUser
from app.schemas.admin.admin_organization_schemas import (
    AdminOrganizationCreateRequest,
    AdminOrganizationListFilters,
    AdminOrganizationListResponse,
    AdminOrganizationResponse,
    AdminOrganizationTreeResponse,
    AdminOrganizationUpdateRequest,
)
from app.schemas.common import OrderSchema, PaginationParams
from app.schemas.response import Response
from app.services.admin.admin_organization_service import AdminOrganizationService


def get_organization_order_schema(
    order_by: str = Query(
        "",
        description="排序字段，多个字段用逗号分隔，字段名前加-表示降序，如：-sort,id。可用字段：id,name,sort",
    )
) -> OrderSchema:
    """获取组织排序Schema"""
    return OrderSchema.with_allowed_fields(allowed_fields={"id", "name", "sort"}, order_by=order_by)


router = APIRouter(prefix="/admin/organization", tags=["管理后台-组织机构管理"])


@router.post(
    "/create", summary="创建组织", description="创建新的组织机构", response_model=Response[AdminOrganizationResponse]
)
async def create_organization(
    data: AdminOrganizationCreateRequest,
    current_user: CurrentUser,
) -> Response[AdminOrganizationResponse]:
    """创建组织机构"""
    result = await AdminOrganizationService.create(data, current_user)
    return Response.success(data=result)


@router.post(
    "/update", summary="更新组织", description="更新组织机构信息", response_model=Response[AdminOrganizationResponse]
)
async def update_organization(
    current_user: CurrentUser,
    org_id: int = Query(..., description="组织ID", gt=0),
    data: AdminOrganizationUpdateRequest = None,
) -> Response[AdminOrganizationResponse]:
    """更新组织机构"""
    result = await AdminOrganizationService.update(org_id, data, current_user)
    return Response.success(data=result)


@router.post("/delete", summary="删除组织", description="删除组织机构", response_model=Response)
async def delete_organization(
    current_user: CurrentUser,
    org_id: int = Query(..., description="组织ID", gt=0),
) -> Response:
    """删除组织机构"""
    await AdminOrganizationService.delete(org_id, current_user)
    return Response.success()


@router.get(
    "/detail",
    summary="获取组织详情",
    description="获取组织机构详细信息",
    response_model=Response[AdminOrganizationResponse],
)
async def get_organization_detail(
    org_id: int = Query(..., description="组织ID", gt=0),
) -> Response[AdminOrganizationResponse]:
    """获取组织机构详情"""
    result = await AdminOrganizationService.get_detail(org_id)
    return Response.success(data=result)


@router.get(
    "/list",
    summary="获取组织列表",
    description="获取组织机构列表，支持分页、排序和筛选",
    response_model=Response[AdminOrganizationListResponse],
)
async def get_organization_list(
    pagination: PaginationParams = Depends(),
    filters: AdminOrganizationListFilters = Depends(),
    order: OrderSchema = Depends(get_organization_order_schema),
) -> Response[AdminOrganizationListResponse]:
    """获取组织机构列表"""
    result = await AdminOrganizationService.get_list(
        filters=filters,
        order=order,
        pagination=pagination,
    )
    return Response.success(data=result)


@router.get(
    "/tree",
    summary="获取组织树",
    description="获取组织机构树形结构",
    response_model=Response[AdminOrganizationTreeResponse],
)
async def get_organization_tree() -> Response[AdminOrganizationTreeResponse]:
    """获取组织机构树"""
    result = await AdminOrganizationService.get_tree()
    return Response.success(data=result)
